#include <string.h>

#include <linux/sched.h>
#include <linux/kernel.h>

struct m_inode inode_table[NR_INODE]={{0, }, };

static void read_inode(struct m_inode *inode);

/**
 * @param dev 设备号
 * @param nr  inode号
 */
struct m_inode *iget(int dev, int nr)
{
    struct m_inode *inode;

    if (!dev)
        panic("iget with dev == 0");
    
    inode = inode_table;
    inode->i_dev = dev;
    inode->i_num = nr;
    read_inode(inode);
    return inode;
}

static void read_inode(struct m_inode *inode)
{
    struct super_block *sb;
    struct buffer_head *bh;
    int block;

    sb = get_super(inode->i_dev);
    block = 2 + sb->s_imap_blocks + sb->s_zmap_blocks + (inode->i_num - 1) / INODES_PER_BLOCK;  // 计算inode所在的逻辑块
    bh = bread(inode->i_dev, block);
    if (!bh)
        panic("unable to read i-node block");
    __asm__("cld"::);
    *(struct d_inode *)inode = ((struct d_inode *)bh->b_data)[(inode->i_num - 1) % INODES_PER_BLOCK];
    brelse(bh);
}